package ru.ifmo.tanya.util;

import java.io.*;


public class StreamUtil {

    private static final int BUFFER_LENGTH = 4096;

    public static long transferData(final InputStream in, final OutputStream out) throws IOException {
      final byte[] buffer = new byte[BUFFER_LENGTH];
      final InputStream bufferedIn = new BufferedInputStream(in);
      final OutputStream bufferedOut = new BufferedOutputStream(out);
      int read;
      long transfered = 0;
      while ((read = bufferedIn.read(buffer)) != -1) {
        bufferedOut.write(buffer, 0, read);
        transfered += read;
      }
      bufferedOut.flush();
      return transfered;
    }

    public static String readStream(InputStream stream) throws IOException {
        StringBuilder result = new StringBuilder();
        BufferedReader in = new BufferedReader(new InputStreamReader(stream));
        while (in.ready()) {
            result.append(in.readLine());
        }
        return result.toString();
    }

    public static void printToStream(String s, OutputStream stream) {
        PrintWriter out = new PrintWriter(stream);
        out.println(s);
        out.close();
    }
}
